package com.crm.manager.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crm.common.core.domain.R;
import com.crm.common.core.page.TableDataInfo;
import com.crm.manager.domain.Clue;
import com.crm.manager.domain.dto.*;
import com.crm.manager.result.PageResult;
import com.crm.manager.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/clues/clue")
public class ClueController {

    @Autowired
    private ClueService clueService;

    // 转商机
    @PutMapping("/changeBusiness/{id}")
    public R changeBusiness(@PathVariable Long id){
        log.info("转商机,接收到的参数是{}",id);
        clueService.changeBusiness(id);
        return R.ok();
    }

    // 新增线索
    @PostMapping
    public R add(@RequestBody ClueDto clueDto){
        log.info("新增线索,接受到的参数是{}",clueDto);
        clueService.add(clueDto);
        return R.ok();
    }


    // 分页查询
    @GetMapping("/list")
    public TableDataInfo pageQuery(CluePageQueryDto dto){
        log.info("分页查询,接受到的参数是{}",dto);
        PageResult p = clueService.pageQuery(dto);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setRows(p.getRows());
        tableDataInfo.setTotal(p.getTotal());
        return tableDataInfo;
    }

    // 根据id查询线索
    @GetMapping("/{id}")
    public R<Clue> selectById(@PathVariable Long id){
        log.info("根据id查询线索,接受到的参数是{}",id);
        return R.ok(clueService.getOne(Wrappers.<Clue>lambdaQuery().eq(Clue::getId,id)));
    }

    // 分配线索
    @PutMapping("/assignment")
    public R assignment(@RequestBody AssignmentDto assignmentDto){
        log.info("分配线索,接受到的参数{}",assignmentDto);
        clueService.assignment(assignmentDto);
        return R.ok();
    }

    // 线索池的分页查询
    @GetMapping("/pool")
    public TableDataInfo poolPageQuery(CluePoolPageQueryDto dto){
        log.info("线索池的分页查询,接收到的参数是{}",dto);
        return clueService.poolPageQuery(dto);
    }

    // 伪线索
    @PutMapping("/false/{id}")
    public R turnFalseClue(@PathVariable Long id, @RequestBody FalseClueDto falseClueDto){
        log.info("伪线索,接收到的参数是id:{},falseClueDto:{}",id,falseClueDto);
        clueService.turnFalseClue(id,falseClueDto);
        return R.ok();
    }

    // 批量获取
    @PutMapping("/gain")
    public R gain (@RequestBody AssignmentDto assignmentDto){
        log.info("批量获取,接收到的参数是{}",assignmentDto);
        clueService.gain(assignmentDto);
        return R.ok();
    }
}
